Shader "Unlit/Lesson85_GlassBase"
{
     Properties
    {
        //Ö÷ÎÆÀí
        _MainTex("MainTex", 2D) = ""{}
        //Á¢·½ÌåÎÆÀí
        _Cube("Cubemap", Cube) = ""{}
        //ÕÛÉä³Ì¶È 0~1 0´ú±íÍêÈ«·´Éä£¨ÍêÈ«²»ÕÛÉä£©1´ú±íÍêÈ«ÕÛÉä£¨Í¸Ã÷Ð§¹û Ïàµ±ÓÚ¹âÈ«²¿½øÈëÁËÄÚ²¿£©
        _RefractAmount("RefractAmount", Range(0,1)) = 1
    }
    SubShader
    {
        //½«äÖÈ¾¶ÓÁÐ¸ÄÎªÍ¸Ã÷µÄ Ä¿µÄÊÇÈÃ²£Á§¶ÔÏó ÖÍºóäÖÈ¾
        //ÄÜ¹»²¶»ñµ½Ö®Ç°ÕýÈ·µÄÆÁÄ»Í¼Ïñ
        Tags{"RenderType"="Opaque" "Queue"="Transparent"}
        //Ê¹ÓÃËüÀ´²¶»ñµ±Ç°ÆÁÄ»ÄÚÈÝ ²¢´æ´¢µ½Ä¬ÈÏµÄäÖÈ¾ÎÆÀí±äÁ¿ÖÐ
        GrabPass{}

        Pass
        {
            Tags{"LightMode"="ForwardBase"}
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
            #include "Lighting.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;
            samplerCUBE _Cube;
            float _RefractAmount;
            //GrabPassÄ¬ÈÏ´æ´¢µÄÎÆÀí±äÁ¿ Õâ¸öÊÇ¹æÔò
            sampler2D _GrabTexture;

            struct v2f
            {
                float4 pos:SV_POSITION;//²Ã¼ô¿Õ¼äÏÂµÄ¶¥µã×ø±ê
                //ÓÃÓÚ´æ´¢´ÓÆÁÄ»Í¼ÏñÖÐ²ÉÑùµÄ×ø±ê£¨¶¥µãÏà¶ÔÓÚÆÁÄ»µÄÎ»ÖÃ£©
                float4 grabPos:TEXCOORD0;
                //ÓÃÓÚÔÚÑÕÉ«ÎÆÀíÖÐ²ÉÑùµÄUV×ø±ê
                float2 uv:TEXCOORD1;
                //ÊÀ½ç¿Õ¼äÏÂµÄ·´ÉäÏòÁ¿
                //ÎÒÃÇ½«°Ñ·´ÉäÏòÁ¿µÄ¼ÆËã·ÅÔÚ¶¥µã×ÅÉ«Æ÷º¯ÊýÖÐ ½ÚÔ¼ÐÔÄÜ ±íÏÖÐ§¹ûÒ²²»»áÌ«²î ÈâÑÛ¼¸ºõ·Ö±æ²»³öÀ´
                float3 worldRefl:TEXCOORD2;
            };

            v2f vert(appdata_base v)
            {
                v2f o;
                //¶¥µã×ø±ê×ª»»
                o.pos = UnityObjectToClipPos(v.vertex);
                //ÆÁÄ»×ø±ê×ª»»Ïà¹ØµÄÄÚÈÝ
                o.grabPos = ComputeGrabScreenPos(o.pos);
                //uv×ø±ê¼ÆËãÏà¹ØµÄÄÚÈÝ
                o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
                //¼ÆËã·´Éä¹âÏòÁ¿
                //1.¼ÆËãÊÀ½ç¿Õ¼äÏÂ·¨ÏßÏòÁ¿
                float3 worldNormal = UnityObjectToWorldNormal(v.normal);
                //2.ÊÀ½ç¿Õ¼äÏÂµÄ¶¥µã×ø±ê
                fixed3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                //3.¼ÆËãÊÓ½Ç·½Ïò ÄÚ²¿ÊÇÓÃÉãÏñ»úÎ»ÖÃ - ÊÀ½ç×ø±êÎ»ÖÃ 
                fixed3 worldViewDir = UnityWorldSpaceViewDir(worldPos);
                //4.¼ÆËã·´ÉäÏòÁ¿
                o.worldRefl = reflect(-worldViewDir, worldNormal);

                return o;
            }

            fixed4 frag(v2f i):SV_TARGET
            {
                //·´ÉäÑÕÉ«Ïà¹ØµÄ¼ÆËã »áµþ¼ÓÖ÷ÎÆÀíÑÕÉ«
                //¶ÔÑÕÉ«ÎÆÀí½øÐÐ²ÉÑù
                fixed4 mainTex = tex2D(_MainTex, i.uv);
                //½«·´ÉäÑÕÉ«ºÍÖ÷ÎÆÀíÑÕÉ«½øÐÐµþ¼Ó
                fixed4 reflColor = texCUBE(_Cube, i.worldRefl) * mainTex;
                
                //ÕÛÉäÏà¹ØµÄÑÕÉ«
                //ÆäÊµ¾ÍÊÇ´ÓÎÒÃÇ×¥È¡µÄ ÆÁÄ»äÖÈ¾ÎÆÀíÖÐ½øÐÐ²ÉÑù ²ÎÓë¼ÆËã
                //×¥È¡ÎÆÀíÖÐµÄÑÕÉ«ÐÅÏ¢ Ïàµ±ÓÚÊÇÕâ¸ö²£Á§¶ÔÏóºóÃæµÄÑÕÉ«

                //ÏëÒªÓÐÕÛÉäÐ§¹û ¿ÉÒÔÔÚ²ÉÑùÖ®Ç° ½øÐÐxyÆÁÄ»×ø±êµÄÆ«ÒÆ
                float2 offset = 1 - _RefractAmount;
                //xyÆ«ÒÆÒ»¸öÎ»ÖÃ
                i.grabPos.xy = i.grabPos.xy - offset/10; 

                //ÀûÓÃÍ¸ÊÓ³ý·¨ ½«ÆÁÄ»×ø±ê×ª»»µ½ 0~1·¶Î§ÄÚ È»ºóÔÙ½øÐÐ²ÉÑù
                fixed2 screenUV = i.grabPos.xy / i.grabPos.w;
                //´Ó²¶»ñµÄäÖÈ¾ÎÆÀíÖÐ½øÐÐ²ÉÑù »ñÈ¡ºóÃæµÄÑÕÉ«
                fixed4 grabColor = tex2D(_GrabTexture, screenUV);

                //ÕÛÉä³Ì¶È 0~1 0´ú±íÍêÈ«·´Éä£¨ÍêÈ«²»ÕÛÉä£©1´ú±íÍêÈ«ÕÛÉä£¨Í¸Ã÷Ð§¹û Ïàµ±ÓÚ¹âÈ«²¿½øÈëÁËÄÚ²¿£©
                float4 color = reflColor*(1 - _RefractAmount) + grabColor * _RefractAmount;

                return color;
            }

            ENDCG
        }
    }
}
